<!DOCTYPE HTML>
<html lang="zh-CN">


<head>
    <meta charset="utf-8">
    <meta name="keywords" content="第13章 xadmin的进阶开发, 湮灭星空,博客,Python,后端">
    <meta name="description" content="介绍xadmin更进阶的开发， 加深对xadmin的理解， 让整个后台管理系统完成更加细节的定制， 包括userprofile的注册、 导航栏icon的修改、 django ueditor富文本编辑器的集成、 xadmin的插件制作。本章会">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <meta name="renderer" content="webkit|ie-stand|ie-comp">
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <title>第13章 xadmin的进阶开发 | 湮灭星空</title>
    <link rel="icon" type="image/png" href="/favicon.png">

    <link rel="stylesheet" type="text/css" href="/libs/awesome/css/all.css">
    <link rel="stylesheet" type="text/css" href="/libs/materialize/materialize.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/aos/aos.css">
    <link rel="stylesheet" type="text/css" href="/libs/animate/animate.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/lightGallery/css/lightgallery.min.css">
    <link rel="stylesheet" type="text/css" href="/css/matery.css">
    <link rel="stylesheet" type="text/css" href="/css/my.css">

    <script src="/libs/jquery/jquery.min.js"></script>

<link rel="alternate" href="/atom.xml" title="湮灭星空" type="application/atom+xml">
</head>

<body>
    <header class="navbar-fixed">
    <nav id="headNav" class="bg-color nav-transparent">
        <div id="navContainer" class="nav-wrapper head-container">
            <div class="brand-logo">
                <a href="/" class="waves-effect waves-light">
                    
                    <img src="/medias/logo.png" class="logo-img" alt="LOGO">
                    
                    <span class="logo-span">湮灭星空</span>
                </a>
            </div>
            

<a href="#" data-target="mobile-nav" class="sidenav-trigger button-collapse"><i class="fas fa-bars"></i></a>
<ul class="right nav-menu">
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/" class="waves-effect waves-light">
      
      <i class="fas fa-home" style="zoom: 0.6;"></i>
      
      <span>首页</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/tags" class="waves-effect waves-light">
      
      <i class="fas fa-tags" style="zoom: 0.6;"></i>
      
      <span>标签</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/categories" class="waves-effect waves-light">
      
      <i class="fas fa-bookmark" style="zoom: 0.6;"></i>
      
      <span>分类</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/archives" class="waves-effect waves-light">
      
      <i class="fas fa-archive" style="zoom: 0.6;"></i>
      
      <span>归档</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/about" class="waves-effect waves-light">
      
      <i class="fas fa-user-circle" style="zoom: 0.6;"></i>
      
      <span>关于</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/contact" class="waves-effect waves-light">
      
      <i class="fas fa-comments" style="zoom: 0.6;"></i>
      
      <span>留言板</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/friends" class="waves-effect waves-light">
      
      <i class="fas fa-address-book" style="zoom: 0.6;"></i>
      
      <span>友链</span>
    </a>
    
  </li>
  
  <li>
    <a href="#searchModal" class="modal-trigger waves-effect waves-light">
      <i id="searchIcon" class="fas fa-search" title="搜索" style="zoom: 0.85;"></i>
    </a>
  </li>
</ul>

<div id="mobile-nav" class="side-nav sidenav">

    <div class="mobile-head bg-color">
        
        <img src="/medias/logo.png" class="logo-img circle responsive-img">
        
        <div class="logo-name">湮灭星空</div>
        <div class="logo-desc">
            
            天下寥寥，苍生涂涂，诸子百家，唯我纵横
            
        </div>
    </div>

    

    <ul class="menu-list mobile-menu-list">
        
        <li class="m-nav-item">
	  
		<a href="/" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-home"></i>
			
			首页
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/tags" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-tags"></i>
			
			标签
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/categories" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-bookmark"></i>
			
			分类
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/archives" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-archive"></i>
			
			归档
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/about" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-user-circle"></i>
			
			关于
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/contact" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-comments"></i>
			
			留言板
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/friends" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-address-book"></i>
			
			友链
		</a>
          
        </li>
        
        
    </ul>
</div>

        </div>

        
    </nav>

</header>

    



<div class="bg-cover pd-header post-cover" style="background-image: url('/medias/featureimages/21.jpg')">
    <div class="container" style="right: 0px;left: 0px;">
        <div class="row">
            <div class="col s12 m12 l12">
                <div class="brand">
                    <div class="description center-align post-title">
                        第13章 xadmin的进阶开发
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>




<main class="post-container content">

    
    <link rel="stylesheet" href="/libs/tocbot/tocbot.css">
<style>
    #articleContent h1::before,
    #articleContent h2::before,
    #articleContent h3::before,
    #articleContent h4::before,
    #articleContent h5::before,
    #articleContent h6::before {
        display: block;
        content: " ";
        height: 100px;
        margin-top: -100px;
        visibility: hidden;
    }

    #articleContent :focus {
        outline: none;
    }

    .toc-fixed {
        position: fixed;
        top: 64px;
    }

    .toc-widget {
        padding-left: 20px;
    }

    .toc-widget .toc-title {
        margin: 35px 0 15px 0;
        padding-left: 17px;
        font-size: 1.5rem;
        font-weight: bold;
        line-height: 1.5rem;
    }

    .toc-widget ol {
        padding: 0;
        list-style: none;
    }

    #toc-content ol {
        padding-left: 10px;
    }

    #toc-content ol li {
        padding-left: 10px;
    }

    #toc-content .toc-link:hover {
        color: #42b983;
        font-weight: 700;
        text-decoration: underline;
    }

    #toc-content .toc-link::before {
        background-color: transparent;
        max-height: 25px;
    }

    #toc-content .is-active-link {
        color: #42b983;
    }

    #toc-content .is-active-link::before {
        background-color: #42b983;
    }

    #floating-toc-btn {
        position: fixed;
        right: 15px;
        bottom: 76px;
        padding-top: 15px;
        margin-bottom: 0;
        z-index: 998;
    }

    #floating-toc-btn .btn-floating {
        width: 48px;
        height: 48px;
    }

    #floating-toc-btn .btn-floating i {
        line-height: 48px;
        font-size: 1.4rem;
    }
</style>
<div class="row">
    <div id="main-content" class="col s12 m12 l9">
        <!-- 文章内容详情 -->
<div id="artDetail">
    <div class="card">
        <div class="card-content article-info">
            <div class="row tag-cate">
                <div class="col s7">
                    
                    <div class="article-tag">
                        
                            <a href="/tags/django/">
                                <span class="chip bg-color">django</span>
                            </a>
                        
                            <a href="/tags/xadmin/">
                                <span class="chip bg-color">xadmin</span>
                            </a>
                        
                            <a href="/tags/python/">
                                <span class="chip bg-color">python</span>
                            </a>
                        
                    </div>
                    
                </div>
                <div class="col s5 right-align">
                    
                    <div class="post-cate">
                        <i class="fas fa-bookmark fa-fw icon-category"></i>
                        
                            <a href="/categories/imooc/" class="post-category">
                                imooc
                            </a>
                        
                    </div>
                    
                </div>
            </div>

            <div class="post-info">
                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-minus fa-fw"></i>发布日期:&nbsp;&nbsp;
                    2019-04-15
                </div>
                

                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-check fa-fw"></i>更新日期:&nbsp;&nbsp;
                    2019-12-06
                </div>
                

                
                <div class="info-break-policy">
                    <i class="far fa-file-word fa-fw"></i>文章字数:&nbsp;&nbsp;
                    3.7k
                </div>
                

                
                <div class="info-break-policy">
                    <i class="far fa-clock fa-fw"></i>阅读时长:&nbsp;&nbsp;
                    15 分
                </div>
                
				
                
                    <div id="busuanzi_container_page_pv" class="info-break-policy">
                        <i class="far fa-eye fa-fw"></i>阅读次数:&nbsp;&nbsp;
                        <span id="busuanzi_value_page_pv"></span>
                    </div>
				
            </div>
            
        </div>
        <hr class="clearfix">
        <div class="card-content article-card-content">
            <div id="articleContent">
                <p>介绍xadmin更进阶的开发， 加深对xadmin的理解， 让整个后台管理系统完成更加细节的定制， 包括userprofile的注册、 导航栏icon的修改、 django ueditor富文本编辑器的集成、 xadmin的插件制作。本章会介绍一款excel的导入插件开发</p>
<h2 id="13-1-userprofile注册以及django的权限管理1"><a class="header-anchor" href="#13-1-userprofile注册以及django的权限管理1"></a>13-1 userprofile注册以及django的权限管理1</h2>
<h2 id="13-2-userprofile注册以及django的权限管理2"><a class="header-anchor" href="#13-2-userprofile注册以及django的权限管理2"></a>13-2 userprofile注册以及django的权限管理2</h2>
<h2 id="13-3-model-icon-只读字段-默认排序设置"><a class="header-anchor" href="#13-3-model-icon-只读字段-默认排序设置"></a>13-3 model_icon, 只读字段，默认排序设置</h2>
<h3 id="换-icon-字段默认排序-字段只读-字段隐藏"><a class="header-anchor" href="#换-icon-字段默认排序-字段只读-字段隐藏"></a>换 icon、字段默认排序、字段只读、字段隐藏</h3>
<p>要设置哪个 model 就在对应 model 的 Admin 下设置相应字段，示例：imooc/apps/courses/adminx.py 内：</p>
<pre><code class="language-python">class CourseAdmin:
    list_display = [&quot;name&quot;, &quot;description&quot;, &quot;detail&quot;, &quot;degree&quot;, &quot;learn_times&quot;, &quot;students&quot;, 							&quot;image&quot;, &quot;click_nums&quot;, &quot;fav_nums&quot;, &quot;add_time&quot;]
    list_filter = [&quot;name&quot;, &quot;description&quot;, &quot;detail&quot;, &quot;degree&quot;, &quot;learn_times&quot;, &quot;students&quot;, &quot;image&quot;, 						&quot;click_nums&quot;, &quot;fav_nums&quot;, &quot;add_time&quot;]
    search_fields = [&quot;name&quot;, &quot;description&quot;, &quot;detail&quot;, &quot;degree&quot;, &quot;learn_times&quot;, &quot;students&quot;, 							&quot;image&quot;, &quot;click_nums&quot;, &quot;fav_nums&quot;, &quot;add_time&quot;]
    model_icon = 'fa fa-table'  # 设置 icon
    ordering = ['-click_nums']  # 课程按照点击数倒序排序
    readonly_fields = ['click_nums']  # 设置点击数字段只读
    exclude = ['fav_nums']  # 隐藏收藏数
</code></pre>
<h3 id="设置字段搜索"><a class="header-anchor" href="#设置字段搜索"></a>设置字段搜索</h3>
<p><img src="https://klause-blog-pictures.oss-cn-shanghai.aliyuncs.com/2019-05-11-133715.jpg" alt="image-20190325002711558"></p>
<p>添加课程的时候，需要下拉选择课程机构，当机构数目非常多的时候就非常麻烦，比如1000个难道每次1000个拉到底吗？显然不方便。那么可不可是一种搜索的的样式展现呢？比如在框中输入 <code>上</code>自动显示机构名称包含 <code>上</code>的的机构。答案是肯定的，只需要在对应的 <code>Admin</code>设置一个字段就可以了。</p>
<p><strong>这里的课程机构是课程 model 的外键，应该是在外键链接的 modelAdmin 里面设置。</strong></p>
<p>这里是课程机构的下拉，所以在 imooc/apps/organization/adminx.py 内设置：<code>    relfield_style = ['fk-ajax']</code>就可以了。</p>
<pre><code class="language-python">class CourseOrgAdmin:
    list_display = [&quot;id&quot;, &quot;add_time&quot;, &quot;click_nums&quot;, &quot;fav_nums&quot;, &quot;name&quot;, &quot;description&quot;, &quot;image&quot;, 					&quot;address&quot;, &quot;city&quot;]
    list_filter = [&quot;id&quot;, &quot;add_time&quot;, &quot;click_nums&quot;, &quot;fav_nums&quot;, &quot;name&quot;, &quot;description&quot;, &quot;image&quot;, 						&quot;address&quot;, &quot;city&quot;]
    search_fields = [&quot;id&quot;, &quot;add_time&quot;, &quot;click_nums&quot;, &quot;fav_nums&quot;, &quot;name&quot;, &quot;description&quot;, &quot;image&quot;, 					&quot;address&quot;, &quot;city&quot;]
    relfield_style = ['fk-ajax']
</code></pre>
<p><code>fk</code> 的意思就是 <code>ForeignKey</code>，一个 model A，当其他 model B 有外键指向 model A 的时候，它的展现方式是以一种 <code>ajax</code>加载的方式完成的，也就是我们所要的搜索效果。</p>
<p>效果如下：</p>
<p><img src="https://klause-blog-pictures.oss-cn-shanghai.aliyuncs.com/2019-05-11-133717.jpg" alt="image-20190325003322130"></p>
<h2 id="13-4-自定义列表返回数据-同一个model注册两个管理器"><a class="header-anchor" href="#13-4-自定义列表返回数据-同一个model注册两个管理器"></a>13-4 自定义列表返回数据，同一个model注册两个管理器</h2>
<h3 id="课程编辑页添加章节信息和课程资源"><a class="header-anchor" href="#课程编辑页添加章节信息和课程资源"></a>课程编辑页添加章节信息和课程资源</h3>
<p>在 xadmin 管理后台添加课程的时候，需要在课程表里添加课程的基本信息，保存好之后才能在章节表里选择刚才添加的课程添加章节，这样操作起来很麻烦，如果可以在添加课程信息之后可以直接添加章节信息就好了，实际上是可以的。</p>
<p>需要在课程 modeAdmin 类里添加 <code>inlines</code>字段，如图：</p>
<p><img src="https://klause-blog-pictures.oss-cn-shanghai.aliyuncs.com/2019-05-11-133722.jpg" alt="image-20190325010636286"></p>
<p>展示效果如图，而且章节信息还是可以拖动的，像个块一样：</p>
<p><img src="https://klause-blog-pictures.oss-cn-shanghai.aliyuncs.com/2019-05-11-133724.jpg" alt="image-20190325010751757"></p>
<h3 id="缺点"><a class="header-anchor" href="#缺点"></a>缺点</h3>
<ul>
<li>只能做一层嵌套，课程里嵌套章节或者章节里嵌套视频，不能做两层嵌套</li>
</ul>
<p>能这样做是因为课程是章节的外键，同样课程也是课程资源的外键，所以也可以像章节一样添加到课程设置页面。效果图太长了，就不放了。</p>
<p><img src="https://klause-blog-pictures.oss-cn-shanghai.aliyuncs.com/2019-05-11-133727.jpg" alt="image-20190325011347007"></p>
<h3 id="记一个-bug"><a class="header-anchor" href="#记一个-bug"></a>记一个 BUG</h3>
<p>上图中有错误，第二个红框处应该是 <code>inlines = [LessonInline, CourseResourceInline]</code>。</p>
<p><img src="https://klause-blog-pictures.oss-cn-shanghai.aliyuncs.com/2019-05-11-133731.jpg" alt="image-20190325172011610"></p>
<p>不改过来的话，在单个课程的编辑页面就会一直报：</p>
<pre><code class="language-python">the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases
</code></pre>
<p>的错误，翻译过来就是：</p>
<pre><code class="language-python">元类冲突：派生类的元类必须是其所有基类的元类的（非严格）子类
</code></pre>
<p>找了很久一步步往前退才发现的，开始我还以为是在 <code>同一个 model 注册两个管理器</code>这节出错的，没想到在这里马虎了。</p>
<h3 id="同一个-model-注册两个管理器"><a class="header-anchor" href="#同一个-model-注册两个管理器"></a>同一个 model 注册两个管理器</h3>
<p>在首页一个轮播课程，判断课程是否为轮播课程的方法就是课程下有个 <code>is_banner</code>的是 <code>True</code>或 <code>False</code>，在 xadmin 管理后台这个值的设置入口比较深，是在课程编辑页面，如果我们需要对轮播课程和非轮播课程做统一管理就比较麻烦，可以将轮播课程和非轮播课程区分开吗？实际上在数据库里轮播课程和非轮播课程是在一张表里的，这里我们就需要使用两个管理器来管理一张表。</p>
<p>显然我们不能用一个 model 去注册两个管理器，因为注册第二个管理器的时候会报该 model 已经注册的错误，所以我们需要再定义一个 model，比如 BannerCourse，BannerCourse 继承 Course，字段基本不需要改动，需要改的地方就是 Meta 信息，表的名字需要改下，Meta 内有一个 proxy 字段需要设置为 True，这个意思表示该 model 不在数据库内创建数据表，否则 Django 会在数据库再生成一张表。</p>
<p><img src="https://klause-blog-pictures.oss-cn-shanghai.aliyuncs.com/2019-05-11-133732.jpg" alt="image-20190325013239845"></p>
<p>好了现在可以为 BannerCourse 注册管理器了，BannerCourse 管理器的注册跟 Course 是一样的，字段信息全部拷贝过来就可以了。</p>
<p><img src="https://klause-blog-pictures.oss-cn-shanghai.aliyuncs.com/2019-05-11-133735.jpg" alt="image-20190325015257457"></p>
<p>注册好之后打开 xadmin 管理后台一看确实出现了 BannerCourse 管理器页面，但是问题来了，BannerCourse 管理器和 Course 管理器的内容完全一样，那么如何在两个管理器展示不同的内容呢？</p>
<p>只需要在两个管理器模型中重载 <code>queryset</code>方法就可以了， 在 imooc/apps/courses/adminx.py 中：</p>
<pre><code class="language-python">...

class CourseAdmin:
    list_display = [&quot;name&quot;, &quot;description&quot;, &quot;detail&quot;, &quot;degree&quot;, &quot;learn_times&quot;, &quot;students&quot;, &quot;image&quot;, &quot;click_nums&quot;,
                    &quot;fav_nums&quot;, &quot;add_time&quot;]
    list_filter = [&quot;name&quot;, &quot;description&quot;, &quot;detail&quot;, &quot;degree&quot;, &quot;learn_times&quot;, &quot;students&quot;, &quot;image&quot;, &quot;click_nums&quot;,
                   &quot;fav_nums&quot;, &quot;add_time&quot;]
    search_fields = [&quot;name&quot;, &quot;description&quot;, &quot;detail&quot;, &quot;degree&quot;, &quot;learn_times&quot;, &quot;students&quot;, &quot;image&quot;, &quot;click_nums&quot;,
                     &quot;fav_nums&quot;, &quot;add_time&quot;]
    model_icon = 'fa fa-table'  # 设置 icon
    ordering = ['-click_nums']  # 按字段排序
    readonly_fields = ['click_nums']  # 设置只读字段
    exclude = ['fav_nums']  # 隐藏字段
    inlines = [LessonInline, CourseResource]

    def queryset(self):
        qs = super(CourseAdmin, self).queryset()
        qs = qs.filter(is_banner=False)
        return qs


xadmin.site.register(Course, CourseAdmin)


class BannerCourseAdmin:
    list_display = [&quot;name&quot;, &quot;description&quot;, &quot;detail&quot;, &quot;degree&quot;, &quot;learn_times&quot;, &quot;students&quot;, &quot;image&quot;, &quot;click_nums&quot;,
                    &quot;fav_nums&quot;, &quot;add_time&quot;]
    list_filter = [&quot;name&quot;, &quot;description&quot;, &quot;detail&quot;, &quot;degree&quot;, &quot;learn_times&quot;, &quot;students&quot;, &quot;image&quot;, &quot;click_nums&quot;,
                   &quot;fav_nums&quot;, &quot;add_time&quot;]
    search_fields = [&quot;name&quot;, &quot;description&quot;, &quot;detail&quot;, &quot;degree&quot;, &quot;learn_times&quot;, &quot;students&quot;, &quot;image&quot;, &quot;click_nums&quot;,
                     &quot;fav_nums&quot;, &quot;add_time&quot;]
    model_icon = 'fa fa-table'  # 设置 icon
    ordering = ['-click_nums']  # 按字段排序
    readonly_fields = ['click_nums']  # 设置只读字段
    exclude = ['fav_nums']  # 隐藏字段
    inlines = [LessonInline, CourseResource]

    def queryset(self):
        qs = super(BannerCourseAdmin, self).queryset()
        qs = qs.filter(is_banner=True)
        return qs


xadmin.site.register(BannerCourse, BannerCourseAdmin)
...
</code></pre>
<p>重载的 queryset 方法内部调用父类的 queryset 方法，然后做一次 is_banner 筛选的查询，返回查询结果，该结果就会展示在 xadmin 管理后台。</p>
<p>效果展示：</p>
<p><img src="https://klause-blog-pictures.oss-cn-shanghai.aliyuncs.com/2019-05-11-133738.jpg" alt="image-20190325020339935"></p>
<p><img src="https://klause-blog-pictures.oss-cn-shanghai.aliyuncs.com/2019-05-11-133741.jpg" alt="image-20190325020357770"></p>
<h3 id="自定义列表页的返回数据"><a class="header-anchor" href="#自定义列表页的返回数据"></a>自定义列表页的返回数据</h3>
<p>上面通过重载 <code>queryset</code> 方法来过滤 <code>is_banner</code>，其实 <code>queryset</code>内部可以写任何想要做的逻辑，所以功能很强大！</p>
<p>自定义列表页的返回数据一样可以通过重载 <code>queryset</code>方法实现。</p>
<h2 id="13-5-xadmin其他常见功能的使用"><a class="header-anchor" href="#13-5-xadmin其他常见功能的使用"></a>13-5 xadmin其他常见功能的使用</h2>
<h3 id="列表页修改字段"><a class="header-anchor" href="#列表页修改字段"></a>列表页修改字段</h3>
<p>在管理器类添加 <code>list_editable</code>字段就可以了</p>
<pre><code class="language-python">list_editable = ['degree']
</code></pre>
<p><mark>这个效果没做出来，报错了，暂时没解决！</mark></p>
<h3 id="列表页显示类方法调用结果-函数"><a class="header-anchor" href="#列表页显示类方法调用结果-函数"></a>列表页显示类方法调用结果（函数）</h3>
<p>course model 内没有定义章节数字段的，之前课程章节数的获取是在课程 model 下定义了一个 <code>get_chapter_num</code>方法来获取的，那如果需要在 course 管理器列表页显示章节数怎么办呢？</p>
<p>其实对象的方法和属性其实都是一样的，我们只需要在<code>list_display</code>字段内加上 <code>get_chapter_num</code>就可以了。</p>
<pre><code class="language-python">list_display = [&quot;name&quot;, &quot;description&quot;, &quot;degree&quot;, &quot;learn_times&quot;, &quot;students&quot;, &quot;click_nums&quot;,
                    &quot;fav_nums&quot;, &quot;add_time&quot;, &quot;get_chapter_num&quot;]
</code></pre>
<p><img src="https://klause-blog-pictures.oss-cn-shanghai.aliyuncs.com/2019-05-11-133747.jpg" alt="image-20190325023029052"></p>
<p>章节数已经展示出来了，但是名称还是不人性化，接下来该名称，只需要在 couurse model 内定义 <code>get_chapter_num.short_description = '章节数’</code>就可以了：</p>
<pre><code class="language-python">class Course(Base):
	...
	def get_chapter_num(self):
        return self.lesson_set.all().count()

    get_chapter_num.short_description = '章节数'
</code></pre>
<p>效果图：</p>
<p><img src="https://klause-blog-pictures.oss-cn-shanghai.aliyuncs.com/2019-05-11-133753.jpg" alt="image-20190325023419582"></p>
<p>缺点：因为章节数是对象方法调用的结果，跟属性还是有些区别的，所以不能对方法进行排序过滤等操作。</p>
<h3 id="显示自定义的-html-代码"><a class="header-anchor" href="#显示自定义的-html-代码"></a>显示自定义的 html 代码</h3>
<p>上面既然能将对象方法的调用结果显示出来，那么如果在对象的方法内写入 html 代码，比如跳转链接，肯定一样会在管理器页面显示跳转链接。</p>
<pre><code class="language-python">class Course(Base):
    ...
	def go_to(self):
        from django.utils.safestring import mark_safe
        return mark_safe('&lt;a href=&quot;http://www.baidu.com&quot;&gt;跳转&lt;/a&gt;')

    go_to.short_description = '跳转'
    ...
</code></pre>
<blockquote>
<p>这里必须要调用 <code>mark_safe</code>方法，否则 Django 会对字符串中的特殊符号，比如 <code>&gt;</code>做文本转换，显示到浏览器就不是我们设定的样式了，使用 <code>mark_safe</code>是告诉 Django 这段文本是安全的，不需要做转换，这样的话文本才能完整的显示在前端。</p>
</blockquote>
<p>效果图：</p>
<p><img src="https://klause-blog-pictures.oss-cn-shanghai.aliyuncs.com/2019-05-11-133754.jpg" alt="image-20190325024558065"></p>
<h3 id="管理器页面定时刷新功能"><a class="header-anchor" href="#管理器页面定时刷新功能"></a>管理器页面定时刷新功能</h3>
<p>有时候因某些也无需要，工作人员可能需要后台管理器页面能够做到每隔一段时间刷新一次页面，这个功能也是可以实现的，xadmin 给我们做好了，我们只需要添加一个 <code>refresh_times</code>字段就可以了：</p>
<pre><code class="language-python">...
class CourseAdmin:
    ...
    refresh_times = [3, 5, 7, 10]
...
</code></pre>
<p>重启服务器，现在课程管理器页面就会出现定时刷新选项！</p>
<p>效果图：</p>
<p><img src="https://klause-blog-pictures.oss-cn-shanghai.aliyuncs.com/2019-05-11-133757.jpg" alt="image-20190325131753174"></p>
<h3 id="自动更新课程数"><a class="header-anchor" href="#自动更新课程数"></a>自动更新课程数</h3>
<p>我们希望在新增或者编辑了一门课程点击保存的时候可以自动的更新新增课程所属课程机构下面的课程数。实际上这就可以重载 <code>save_model</code>方法：</p>
<pre><code class="language-python">class Course(Base):
    ...
	def save_models(self):
        &quot;&quot;&quot;
        在保存课程的时候统计课程机构的课程数
        &quot;&quot;&quot;
        new_course = self.new_obj
        new_course.save()
        if new_course.course_org is not None:
            course_org = self.course_org
            course_org.course_nums = Course.objects.filter(course_org=course_org)
            course_org.save()
</code></pre>
<p>重启服务器之后，在编辑或者新增课程之后点击保存的时候都会 <code>save_models</code>方法。</p>
<h2 id="13-6-xadmin集成富文本ueditor1"><a class="header-anchor" href="#13-6-xadmin集成富文本ueditor1"></a>13-6 xadmin集成富文本ueditor1</h2>
<ol>
<li>
<p>安装 <code>DjangoUeditor</code>插件</p>
<pre><code class="language-python">pip install DjangoUeditor
</code></pre>
</li>
<li>
<p>在 <code>Django</code> 中安装 <code>DjangoUeditor</code> 在<code>INSTALL_APPS</code>里面增加 <code>DjangoUeditor</code> app，如下：</p>
<pre><code class="language-python"> INSTALLED_APPS = ( #........ 'DjangoUeditor', )
</code></pre>
</li>
<li>
<p>配置 <code>urls.py</code></p>
<pre><code class="language-python">url(r'^ueditor/',include('DjangoUeditor.urls' )),
</code></pre>
</li>
<li>
<p>设置使用 <code>ueditor</code>编辑文本的字段</p>
<pre><code class="language-python">class Course(Base):
	detail = UEditorField(u'内容	', width=600, height=300, imagePath=&quot;courses/ueditor/image/%(basename)s_%(datetime)s.%&quot;,filePath=&quot;courses/ueditor/file/%(basename)s_%(datetime)s.%&quot;, upload_settings={&quot;imageMaxSize&quot;: 1204000}, default='')
</code></pre>
</li>
</ol>
<h2 id="13-7-xadmin集成富文本ueditor2"><a class="header-anchor" href="#13-7-xadmin集成富文本ueditor2"></a>13-7 xadmin集成富文本ueditor2</h2>
<p>集成到 <code>xadmin</code>：</p>
<ol>
<li>
<p>在 imooc/courses/adminx.py 的 <code>CourseAdmin</code>中指明<code>Course</code>模型字段的样式：</p>
<pre><code class="language-python">class CourseAdmin:
	style_fields = {'detail': 'ueditor'}
</code></pre>
<p>这里是指明 <code>detail</code>字段的样式为 <code>ueditor</code>。</p>
</li>
<li>
<p>在 imooc/extra_apps/xadmin/plugins 中添加 <code>ueditor.py</code> 文件：</p>
<pre><code class="language-python"># ueditor.py
from DjangoUeditor.models import UEditorField
from DjangoUeditor.widgets import UEditorWidget
from django.conf import settings

import xadmin
from xadmin.views import BaseAdminPlugin, CreateAdminView, UpdateAdminView


class XadminUEditorWidget(UEditorWidget):
    def __init__(self, **kwargs):
        self.ueditor_options = kwargs
        self.Media.js = None
        super(XadminUEditorWidget, self).__init__(kwargs)


class UeditorPlugin(BaseAdminPlugin):
    def get_filed_style(self, attrs, db_field, style, **kwargs):
        if style == 'ueditor' and isinstance(db_field, UEditorField):
            widget = db_field.formfield().widget
            param = {}
            param.update(widget.ueditor_settings)
            param.update(widget.attrs)
            return dict(widget=XadminUEditorWidget(**param))
        return attrs

    def block_extrahead(self, context, nodes):
        js = '&lt;script type=&quot;text/javascript&quot; src=&quot;{}&quot;&gt;&lt;/script&gt;'.format(
            settings.STATIC_URL + 'ueditor/ueditor.config.js')
        js += '&lt;script type=&quot;text/javascript&quot; src=&quot;{}&quot;&gt;&lt;/script&gt;'.format(
            settings.STATIC_URL + 'ueditor/ueditor.all.min.js')
        nodes.append(js)


xadmin.site.register_plugin(UeditorPlugin, CreateAdminView)
xadmin.site.register_plugin(UeditorPlugin, UpdateAdminView)
</code></pre>
<p>文件中主要就是写了 <code>UeditorPlgin</code>插件，然后将插件分别注册到<strong>新增视图类</strong>和<strong>编辑视图类</strong>，因为富文本编辑器只会出现在这两个试图类中。（相当于修改页面和新增页面）</p>
<p>在<code>UeditorPlgin</code>插件中只定义了两个方法<code>get_filed_style</code>和<code>block_extrahead</code>：</p>
<ul>
<li>
<p><code>get_filed_style</code>：是用来捕捉样式为<code>ueditor</code>的字段的，铺捉到字段之后</p>
</li>
<li>
<p><code>block_extrahead</code>：可以让我们在生成的 html 页面当中加入额外的 <code>js</code> 文件。</p>
</li>
</ul>
</li>
<li>
<p>注册 <code>ueditor</code>插件，在  <code>imooc/extra_apps/xadmin/plugins/__init__.py</code> 中：</p>
<pre><code class="language-python">PLUGINS = (
    &quot;ueditor&quot;,
)
</code></pre>
</li>
</ol>
<h3 id="坑-1：djangoueditor-对-python3-支持不好"><a class="header-anchor" href="#坑-1：djangoueditor-对-python3-支持不好"></a>坑 1：DjangoUeditor 对 python3 支持不好</h3>
<p>理论上应该已经可以使用 <code>DjangoUeditor</code>编辑器了，但是遇到点小插曲，由于 <code>DjangoUeditor</code>是基于 python2.7 的对 python3 的支持有些问题，会导致</p>
<pre><code class="language-python">from widgets import UEditorWidget,AdminUEditorWidget
ImportError: No module named ‘widgets’
</code></pre>
<p>所以搜了下网上的解决办法有两种：</p>
<ol>
<li>修改 <code>widget.py</code>文件</li>
<li>使用网上别人修改好的 <code>DjangoUeditor</code>版本：<a href="https://github.com/twz915/DjangoUeditor3" target="_blank" rel="noopener">https://github.com/twz915/DjangoUeditor3</a></li>
</ol>
<p>采用的是第二种方法：</p>
<ol>
<li>
<p>先卸载之前安装的<code>DjangoUeditor</code></p>
<pre><code class="language-python">pip uninstall DjangoUeditor
</code></pre>
</li>
<li>
<p>从 Github 下载别人修改好的<code>DjangoUeditor</code>的源码，解压从源码安装<code>DjangoUeditor</code>，<code>cd</code> 到解压后的文件夹目录，开启虚拟环境：</p>
<pre><code class="language-python">python  setup.py install安装
</code></pre>
</li>
</ol>
<p>安装成功就可以重启服务器试试看了，发现已经可以使用<code>DjangoUeditor</code>富文本编辑器了。</p>
<h3 id="坑-2：显示效果不对"><a class="header-anchor" href="#坑-2：显示效果不对"></a>坑 2：显示效果不对</h3>
<p>但是还有个问题，就是前端显示的效果不对。实际上这是 Django 对富文本自带的的防御机制，Django 会对某些字符做转义，比如 <code>&gt;</code>，所以我们需要在前端显示的地方关闭告诉 Django 这里的显示不需要转义子字符。</p>
<p>因为是课程的详情字段，所以找到课程详情页面显示详情的部分添加如下代码，将不需要转义的字段包裹起来就好了：</p>
<pre><code class="language-jinja2">{% autoescape off %}
{{ course.detail }}
{% endautoescape %}
</code></pre>
<p>结果前段页面显示良好。</p>
<h2 id="13-8-excel导入插件介绍"><a class="header-anchor" href="#13-8-excel导入插件介绍"></a>13-8 excel导入插件介绍</h2>
<ol>
<li>
<p>编写 <code>excel</code>插件， imooc/extra_apps/xadmin/plugins/excel.py：</p>
<pre><code class="language-python">from django.template import loader

import xadmin
from xadmin.views import ListAdminView, BaseAdminPlugin


class ListImportExcelPlugin(BaseAdminPlugin):
    import_excel = False

    def init_request(self, *args, **kwargs):
        return bool(self.import_excel)

    def block_top_toolbar(self, context, nodes):
	     nodes.append(loader.render_to_string(
	         'xadmin/excel/model_list.top_toolbar.import.html'))

xadmin.site.register_plugin(ListImportExcelPlugin, ListAdminView)
</code></pre>
<ul>
<li><code>init_request</code>：表示是否启用 <code>excel</code>上传功能</li>
<li><code>block_top_toolbar</code>：表示在管理器列表页面上传按钮（这里是替换 <code>top_toolbar</code>的 html 代码块）</li>
</ul>
</li>
<li>
<p>注册 <code>excel</code>插件，<code>imooc/extra_apps/xadmin/plugins/__init__.py</code>：</p>
<pre><code class="language-python">PLUGINS = (
    &quot;excel&quot;,
)
</code></pre>
</li>
<li>
<p>在需要上传 <code>excel</code>功能的管理器视图函数内启用 <code>excel</code>功能并做具体的业务逻辑（即编写<code>post</code>函数）。</p>
<pre><code class="language-python">class CourseAdmin:
	import_excel = True
    def post(self, request, *args, **kwargs):
        if 'excel' in request.FILES:
            # 处理自己的逻辑
            pass
        return super(CourseAdmin, self).post(request, *args, **kwargs)
</code></pre>
<ul>
<li><code>import_excel = True</code>：表示启用<code>excel</code>上传功能；</li>
<li><code>post</code>函数表示对上传的 <code>excel</code>做哪些具体的业务处理。</li>
</ul>
</li>
</ol>
<blockquote>
<p>post 方式上传的文件都保存在 <code>request.FILES</code>字典中</p>
<p><img src="https://klause-blog-pictures.oss-cn-shanghai.aliyuncs.com/2019-05-11-133759.jpg" alt="image-20190325203548942"></p>
</blockquote>

<div id="gitalk-container"></div>
<script src="https://cdn.bootcss.com/blueimp-md5/2.12.0/js/md5.min.js"></script><link rel="stylesheet" href="https://unpkg.com/gitalk/dist/gitalk.css"><script src="https://unpkg.com/gitalk/dist/gitalk.min.js"></script>

		<script>
		var gitalkConfig = {"clientID":"9eb5bc3ac1e1ff3ddac0","clientSecret":"4b7ae28042282281295075c2bf0c97ff1791cfeb","repo":"HexoBlogComments","owner":"Annihilater","admin":["Annihilater"],"distractionFreeMode":false};
	    gitalkConfig.id = md5(location.pathname);
		var gitalk = new Gitalk(gitalkConfig);
	    gitalk.render("gitalk-container");
	    </script>
            </div>
            <hr/>

            

    <div class="reprint" id="reprint-statement">
        
            <div class="reprint__author">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-user">
                        文章作者:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="https://www.klause.cn" rel="external nofollow noreferrer">湮灭星空</a>
                </span>
            </div>
            <div class="reprint__type">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-link">
                        文章链接:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="https://www.klause.cn/2019/04/15/imooc/di-13-zhang-xadmin-de-jin-jie-kai-fa/">https://www.klause.cn/2019/04/15/imooc/di-13-zhang-xadmin-de-jin-jie-kai-fa/</a>
                </span>
            </div>
            <div class="reprint__notice">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-copyright">
                        版权声明:
                    </i>
                </span>
                <span class="reprint-info">
                    本博客所有文章除特別声明外，均采用
                    <a href="https://creativecommons.org/licenses/by/4.0/deed.zh" rel="external nofollow noreferrer" target="_blank">CC BY 4.0</a>
                    许可协议。转载请注明来源
                    <a href="https://www.klause.cn" target="_blank">湮灭星空</a>
                    !
                </span>
            </div>
        
    </div>

    <script async defer>
      document.addEventListener("copy", function (e) {
        let toastHTML = '<span>复制成功，请遵循本文的转载规则</span><button class="btn-flat toast-action" onclick="navToReprintStatement()" style="font-size: smaller">查看</a>';
        M.toast({html: toastHTML})
      });

      function navToReprintStatement() {
        $("html, body").animate({scrollTop: $("#reprint-statement").offset().top - 80}, 800);
      }
    </script>



            <div class="tag_share" style="display: block;">
                <div class="post-meta__tag-list" style="display: inline-block;">
                    
                        <div class="article-tag">
                            
                                <a href="/tags/django/">
                                    <span class="chip bg-color">django</span>
                                </a>
                            
                                <a href="/tags/xadmin/">
                                    <span class="chip bg-color">xadmin</span>
                                </a>
                            
                                <a href="/tags/python/">
                                    <span class="chip bg-color">python</span>
                                </a>
                            
                        </div>
                    
                </div>
                <div class="post_share" style="zoom: 80%; width: fit-content; display: inline-block; float: right; margin: -0.15rem 0;">
                    <link rel="stylesheet" type="text/css" href="/libs/share/css/share.min.css">

<div id="article-share">
    
    
    <div class="social-share" data-sites="twitter,facebook,google,qq,qzone,wechat,weibo,douban,linkedin" data-wechat-qrcode-helper="<p>微信扫一扫即可分享！</p>"></div>
    <script src="/libs/share/js/social-share.min.js"></script>
    

    

</div>

                </div>
            </div>
            
        </div>
    </div>

    

    

    

    

    

    

<article id="prenext-posts" class="prev-next articles">
    <div class="row article-row">
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge left-badge text-color">
                <i class="fas fa-chevron-left"></i>&nbsp;上一篇</div>
            <div class="card">
                <a href="/2019/04/15/imooc/di-11-zhang-shou-ye-quan-ju-gong-neng-xi-jie-he-404-yi-ji-500-ye-mian-pei-zhi/">
                    <div class="card-image">
                        
                        
                        <img src="/medias/featureimages/18.jpg" class="responsive-img" alt="第11章 首页、全局功能细节和404以及500页面配置">
                        
                        <span class="card-title">第11章 首页、全局功能细节和404以及500页面配置</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            本章主要完善整站的实现细节， 如修改点击数、收藏数以及登出功能等。 接着实现系统的首页， 最后配置系统的全局404和500页面
11-1 登出和点击数以及收藏数完善
11-2 首页功能开发1
11-3 首页功能开发2
11-4 404和50
                        
                    </div>
                    <div class="publish-info">
                        <span class="publish-date">
                            <i class="far fa-clock fa-fw icon-date"></i>2019-04-15
                        </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/imooc/" class="post-category">
                                    imooc
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/django/">
                        <span class="chip bg-color">django</span>
                    </a>
                    
                    <a href="/tags/xadmin/">
                        <span class="chip bg-color">xadmin</span>
                    </a>
                    
                    <a href="/tags/python/">
                        <span class="chip bg-color">python</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge right-badge text-color">
                下一篇&nbsp;<i class="fas fa-chevron-right"></i>
            </div>
            <div class="card">
                <a href="/2019/04/15/imooc/di-12-zhang-chang-jian-web-gong-ji-ji-fang-fan/">
                    <div class="card-image">
                        
                        
                        <img src="/medias/featureimages/13.jpg" class="responsive-img" alt="第12章 常见web攻击及防范">
                        
                        <span class="card-title">第12章 常见web攻击及防范</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            本章介绍最常见的sql注入攻击、 xss攻击和csrf攻击的原理以及防护

12-1 sql注入攻击与防范
sql 注入的危害：

非法读取、篡改、删除数据库中的数据
盗取用户各类敏感信息，获取利益
通过修改数据库来修改网页上的内容
注入木
                        
                    </div>
                    <div class="publish-info">
                            <span class="publish-date">
                                <i class="far fa-clock fa-fw icon-date"></i>2019-04-15
                            </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/imooc/" class="post-category">
                                    imooc
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/django/">
                        <span class="chip bg-color">django</span>
                    </a>
                    
                    <a href="/tags/xadmin/">
                        <span class="chip bg-color">xadmin</span>
                    </a>
                    
                    <a href="/tags/python/">
                        <span class="chip bg-color">python</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
    </div>
</article>

</div>



<!-- 代码块功能依赖 -->
<script type="text/javascript" src="/libs/codeBlock/codeBlockFuction.js"></script>

<!-- 代码语言 -->

<script type="text/javascript" src="/libs/codeBlock/codeLang.js"></script>

    
<!-- 代码块复制 -->

<script type="text/javascript" src="/libs/codeBlock/codeCopy.js"></script>


<!-- 代码块收缩 -->

<script type="text/javascript" src="/libs/codeBlock/codeShrink.js"></script>


<!-- 代码块折行 -->

<style type="text/css">
code[class*="language-"], pre[class*="language-"] { white-space: pre !important; }
</style>

    </div>
    <div id="toc-aside" class="expanded col l3 hide-on-med-and-down">
        <div class="toc-widget">
            <div class="toc-title"><i class="far fa-list-alt"></i>&nbsp;&nbsp;目录</div>
            <div id="toc-content"></div>
        </div>
    </div>
</div>

<!-- TOC 悬浮按钮. -->

<div id="floating-toc-btn" class="hide-on-med-and-down">
    <a class="btn-floating btn-large bg-color">
        <i class="fas fa-list-ul"></i>
    </a>
</div>


<script src="/libs/tocbot/tocbot.min.js"></script>
<script>
    $(function () {
        tocbot.init({
            tocSelector: '#toc-content',
            contentSelector: '#articleContent',
            headingsOffset: -($(window).height() * 0.4 - 45),
            // headingsOffset: -205,
            headingSelector: 'h2, h3, h4, h5, h6'
        });

        // modify the toc link href to support Chinese.
        let i = 0;
        let tocHeading = 'toc-heading-';
        $('#toc-content a').each(function () {
            $(this).attr('href', '#' + tocHeading + (++i));
        });

        // modify the heading title id to support Chinese.
        i = 0;
        $('#articleContent').children('h2, h3, h4, h5, h6').each(function () {
            $(this).attr('id', tocHeading + (++i));
        });

        // Set scroll toc fixed.
        let tocHeight = parseInt($(window).height() * 0.4 - 64);
        let $tocWidget = $('.toc-widget');
        $(window).scroll(function () {
            let scroll = $(window).scrollTop();
            /* add post toc fixed. */
            if (scroll > tocHeight) {
                $tocWidget.addClass('toc-fixed');
            } else {
                $tocWidget.removeClass('toc-fixed');
            }
        });

        
        /* 修复文章卡片 div 的宽度. */
        let fixPostCardWidth = function (srcId, targetId) {
            let srcDiv = $('#' + srcId);
            if (srcDiv.length === 0) {
                return;
            }

            let w = srcDiv.width();
            if (w >= 450) {
                w = w + 21;
            } else if (w >= 350 && w < 450) {
                w = w + 18;
            } else if (w >= 300 && w < 350) {
                w = w + 16;
            } else {
                w = w + 14;
            }
            $('#' + targetId).width(w);
        };

        // 切换TOC目录展开收缩的相关操作.
        const expandedClass = 'expanded';
        let $tocAside = $('#toc-aside');
        let $mainContent = $('#main-content');
        $('#floating-toc-btn .btn-floating').click(function () {
            if ($tocAside.hasClass(expandedClass)) {
                $tocAside.removeClass(expandedClass).hide();
                $mainContent.removeClass('l9');
            } else {
                $tocAside.addClass(expandedClass).show();
                $mainContent.addClass('l9');
            }
            fixPostCardWidth('artDetail', 'prenext-posts');
        });
        
    });
</script>

    

</main>



    <footer class="page-footer bg-color">
    <div class="container row center-align">
        <div class="col s12 m8 l8 copy-right">
            Copyright&nbsp;&copy;
            <span id="year">年份</span>
            <a href="https://www.klause.cn" target="_blank">湮灭星空</a>
            |&nbsp;Powered by&nbsp;<a href="https://hexo.io/" target="_blank">Hexo</a>
            |&nbsp;Theme&nbsp;<a href="https://github.com/blinkfox/hexo-theme-matery" target="_blank">Matery</a>
            <br>
            
            &nbsp;<i class="fas fa-chart-area"></i>&nbsp;站点总字数:&nbsp;<span
                class="white-color">199.2k</span>&nbsp;字
            
            
            
            
            
            
            <span id="busuanzi_container_site_pv">
                |&nbsp;<i class="far fa-eye"></i>&nbsp;总访问量:&nbsp;<span id="busuanzi_value_site_pv"
                    class="white-color"></span>&nbsp;次
            </span>
            
            
            <span id="busuanzi_container_site_uv">
                |&nbsp;<i class="fas fa-users"></i>&nbsp;总访问人数:&nbsp;<span id="busuanzi_value_site_uv"
                    class="white-color"></span>&nbsp;人
            </span>
            
            <br>
            
            <span id="sitetime">载入运行时间...</span>
            <script>
                function siteTime() {
                    window.setTimeout("siteTime()", 1000);
                    var seconds = 1000;
                    var minutes = seconds * 60;
                    var hours = minutes * 60;
                    var days = hours * 24;
                    var years = days * 365;
                    var today = new Date();
                    var startYear = "2019";
                    var startMonth = "6";
                    var startDate = "28";
                    var startHour = "0";
                    var startMinute = "0";
                    var startSecond = "0";
                    var todayYear = today.getFullYear();
                    var todayMonth = today.getMonth() + 1;
                    var todayDate = today.getDate();
                    var todayHour = today.getHours();
                    var todayMinute = today.getMinutes();
                    var todaySecond = today.getSeconds();
                    var t1 = Date.UTC(startYear, startMonth, startDate, startHour, startMinute, startSecond);
                    var t2 = Date.UTC(todayYear, todayMonth, todayDate, todayHour, todayMinute, todaySecond);
                    var diff = t2 - t1;
                    var diffYears = Math.floor(diff / years);
                    var diffDays = Math.floor((diff / days) - diffYears * 365);
                    var diffHours = Math.floor((diff - (diffYears * 365 + diffDays) * days) / hours);
                    var diffMinutes = Math.floor((diff - (diffYears * 365 + diffDays) * days - diffHours * hours) /
                        minutes);
                    var diffSeconds = Math.floor((diff - (diffYears * 365 + diffDays) * days - diffHours * hours -
                        diffMinutes * minutes) / seconds);
                    if (startYear == todayYear) {
                        document.getElementById("year").innerHTML = todayYear;
                        document.getElementById("sitetime").innerHTML = "本站已安全运行 " + diffDays + " 天 " + diffHours +
                            " 小时 " + diffMinutes + " 分钟 " + diffSeconds + " 秒";
                    } else {
                        document.getElementById("year").innerHTML = startYear + " - " + todayYear;
                        document.getElementById("sitetime").innerHTML = "本站已安全运行 " + diffYears + " 年 " + diffDays +
                            " 天 " + diffHours + " 小时 " + diffMinutes + " 分钟 " + diffSeconds + " 秒";
                    }
                }
                setInterval(siteTime, 1000);
            </script>
            
            <br>
            
            <span id="icp"><img src="/medias/icp.png" style="vertical-align: text-bottom;" />
                <a href="http://www.beian.miit.gov.cn" target="_blank">皖ICP备18005729号-1</a>
            </span>
            
        </div>
        <div class="col s12 m4 l4 social-link social-statis">
    <a href="https://github.com/Annihilater" class="tooltipped" target="_blank" data-tooltip="访问我的GitHub" data-position="top" data-delay="50">
        <i class="fab fa-github"></i>
    </a>



    <a href="mailto:yanmiexingkong@gmail.com" class="tooltipped" target="_blank" data-tooltip="邮件联系我" data-position="top" data-delay="50">
        <i class="fas fa-envelope-open"></i>
    </a>





    <a href="https://twitter.com/" class="tooltipped" target="_blank" data-tooltip="关注我的Twitter: https://twitter.com/" data-position="top" data-delay="50">
        <i class="fab fa-twitter"></i>
    </a>







    <a href="https://www.zhihu.com/" class="tooltipped" target="_blank" data-tooltip="关注我的知乎: https://www.zhihu.com/" data-position="top" data-delay="50">
        <i class="fab fa-zhihu1">知</i>
    </a>



    <a href="/atom.xml" class="tooltipped" target="_blank" data-tooltip="RSS 订阅" data-position="top" data-delay="50">
        <i class="fas fa-rss"></i>
    </a>

</div>
    </div>
</footer>

<div class="progress-bar"></div>

    <!-- 搜索遮罩框 -->
<div id="searchModal" class="modal">
    <div class="modal-content">
        <div class="search-header">
            <span class="title"><i class="fas fa-search"></i>&nbsp;&nbsp;搜索</span>
            <input type="search" id="searchInput" name="s" placeholder="请输入搜索的关键字"
                   class="search-input">
        </div>
        <div id="searchResult"></div>
    </div>
</div>

<script src="/js/search.js"></script>
<script type="text/javascript">
$(function () {
    searchFunc("/" + "search.xml", 'searchInput', 'searchResult');
});
</script>
    <!-- 回到顶部按钮 -->
<div id="backTop" class="top-scroll">
    <a class="btn-floating btn-large waves-effect waves-light" href="#!">
        <i class="fas fa-arrow-up"></i>
    </a>
</div>


    <script src="/libs/materialize/materialize.min.js"></script>
    <script src="/libs/masonry/masonry.pkgd.min.js"></script>
    <script src="/libs/aos/aos.js"></script>
    <script src="/libs/scrollprogress/scrollProgress.min.js"></script>
    <script src="/libs/lightGallery/js/lightgallery-all.min.js"></script>
    <script src="/js/matery.js"></script>

    <!-- Global site tag (gtag.js) - Google Analytics -->


    <!-- Baidu Analytics -->

    <!-- Baidu Push -->

<script>
    (function () {
        var bp = document.createElement('script');
        var curProtocol = window.location.protocol.split(':')[0];
        if (curProtocol === 'https') {
            bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
        } else {
            bp.src = 'http://push.zhanzhang.baidu.com/push.js';
        }
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(bp, s);
    })();
</script>

    
    <script src="/libs/others/clicklove.js" async="async"></script>
    
    
    <script async src="/libs/others/busuanzi.pure.mini.js"></script>
    

    

    

    

    

    
    <script type="text/javascript" src="/libs/background/ribbon-dynamic.js" async="async"></script>
    
    
    
    <script src="/libs/instantpage/instantpage.js" type="module"></script>
    

</body>

</html>
